Linear regression

In [1]:
insurance <- read.csv("insurance.csv", stringsAsFactors = TRUE)

What is kNN

In [2]:
str(insurance)
'data.frame':	1338 obs. of  7 variables:
 $ age     : int  19 18 28 33 32 31 46 37 37 60 ...
 $ sex     : Factor w/ 2 levels "female","male": 1 2 2 2 2 1 1 1 2 1 ...
 $ bmi     : num  27.9 33.8 33 22.7 28.9 ...
 $ children: int  0 1 3 0 0 0 1 3 2 0 ...
 $ smoker  : Factor w/ 2 levels "no","yes": 2 1 1 1 1 1 1 1 1 1 ...
 $ region  : Factor w/ 4 levels "northeast","northwest",..: 4 3 3 2 2 3 3 2 1 2 ...
 $ charges : num  16885 1726 4449 21984 3867 ...
In [3]:
summary(insurance$charges)
Out[3]:
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1122    4740    9382   13270   16640   63770 
In [4]:
hist(insurance$charges)
In [5]:
table(insurance$region)
Out[5]:
northeast northwest southeast southwest 
      324       325       364       325 
In [6]:
cor(insurance[c("age", "bmi", "children", "charges")])
Out[6]:
agebmichildrencharges
age1.00000000.10927190.04246900.2990082
bmi0.10927191.00000000.01275890.1983410
children0.042469000.012758901.000000000.06799823
charges0.299008190.198340970.067998231.00000000
In [7]:
pairs(insurance[c("age", "bmi", "children", "charges")])
In [10]:
ins_model <- lm(charges ~ age + children + bmi + sex +
smoker + region, data = insurance)
In [12]:
ins_model
Out[12]:
Call:
lm(formula = charges ~ age + children + bmi + sex + smoker + 
    region, data = insurance)

Coefficients:
    (Intercept)              age         children              bmi  
       -11938.5            256.9            475.5            339.2  
        sexmale        smokeryes  regionnorthwest  regionsoutheast  
         -131.3          23848.5           -353.0          -1035.0  
regionsouthwest  
         -960.1  
In [13]:
summary(ins_model)
Out[13]:
Call:
lm(formula = charges ~ age + children + bmi + sex + smoker + 
    region, data = insurance)

Residuals:
     Min       1Q   Median       3Q      Max 
-11304.9  -2848.1   -982.1   1393.9  29992.8 

Coefficients:
                Estimate Std. Error t value Pr(>|t|)    
(Intercept)     -11938.5      987.8 -12.086  < 2e-16 ***
age                256.9       11.9  21.587  < 2e-16 ***
children           475.5      137.8   3.451 0.000577 ***
bmi                339.2       28.6  11.860  < 2e-16 ***
sexmale           -131.3      332.9  -0.394 0.693348    
smokeryes        23848.5      413.1  57.723  < 2e-16 ***
regionnorthwest   -353.0      476.3  -0.741 0.458769    
regionsoutheast  -1035.0      478.7  -2.162 0.030782 *  
regionsouthwest   -960.0      477.9  -2.009 0.044765 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 6062 on 1329 degrees of freedom
Multiple R-squared:  0.7509,	Adjusted R-squared:  0.7494 
F-statistic: 500.8 on 8 and 1329 DF,  p-value: < 2.2e-16
In [ ]: